package org.erp.product.domain.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.erp.common.context.StockLockContext;
import org.erp.common.enums.DeviceInventoryEvent;
import org.erp.product.domain.entity.ConnectLog;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author libin
 * @version 1.0
 * @description:
 * @date 2025/11/19 17:12
 **/
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class AsyncMessageStatusDto implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 需要更新的库存ID列表
     */
    private List<WarehouseDto> inventoryList;
    /**
     * 批次id
     */
    private String batchId;
    /**
     * uuid
     */
    private String uuid;
    /**
     * 目标状态事件（枚举）
     */
    private DeviceInventoryEvent targetEvent;
    /**
     * 调拨类型
     */
    private Integer transferType;
    /**
     * 操作人ID
     */
    private Long operateId;
    /**
     * 操作人姓名
     */
    private String operateName;
    /**
     * 部门id
     */
    private Long deptId;
    /**
     * 部门名称
     */
    private String deptName;
    /**
     * 转交人id
     */
    private Long transferUserId;
    /**
     * 转交人名称
     */
    private String transferUserName;

    /**
     * 构建调拨实体
     * @param transferDto
     * @param dickerDtoList
     * @return
     */
    public static AsyncMessageStatusDto buildTransferAsyncMessageStatusDto(TransferDto transferDto, List<DickerDto> dickerDtoList) {
        AsyncMessageStatusDto asyncMessageStatusDto = new AsyncMessageStatusDto();
        List<WarehouseDto> warehouseDtoList = new ArrayList<>();
        dickerDtoList.forEach(c -> {
            WarehouseDto warehouseDto = new WarehouseDto();
            warehouseDto.setInventoryId(c.getStockId());
            warehouseDto.setProcurementPremium(c.getDickerPrice());
            warehouseDtoList.add(warehouseDto);
        });
        asyncMessageStatusDto.setInventoryList(warehouseDtoList);
        asyncMessageStatusDto.setBatchId(StockLockContext.getBatchKey());
        asyncMessageStatusDto.setUuid(UUID.randomUUID().toString());
        asyncMessageStatusDto.setTargetEvent(DeviceInventoryEvent.ALLOCATE);
        asyncMessageStatusDto.setTransferType(transferDto.getTransferType());
        asyncMessageStatusDto.setOperateId(transferDto.getOperationId());
        asyncMessageStatusDto.setOperateName(transferDto.getOperationName());
        asyncMessageStatusDto.setDeptId(transferDto.getDeptId());
        asyncMessageStatusDto.setDeptName(transferDto.getDeptName());
        asyncMessageStatusDto.setTransferUserId(transferDto.getTransferId());
        asyncMessageStatusDto.setTransferUserName(transferDto.getTransferName());
        return asyncMessageStatusDto;
    }

    public static AsyncMessageStatusDto buildActionAsyncMessageStatusDto(List<ConnectLog> warehouseList, DeviceInventoryEvent deviceInventoryEvent) {
        AsyncMessageStatusDto asyncMessageStatusDto = new AsyncMessageStatusDto();
        List<WarehouseDto> warehouseDtoList = new ArrayList<>();

        for (ConnectLog c : warehouseList) {
            WarehouseDto warehouseDto = new WarehouseDto();
            warehouseDto.setInventoryId(c.getStockId());
            warehouseDtoList.add(warehouseDto);
        }
        asyncMessageStatusDto.setBatchId(StockLockContext.getBatchKey());
        asyncMessageStatusDto.setUuid(UUID.randomUUID().toString());
        asyncMessageStatusDto.setTargetEvent(deviceInventoryEvent);
        asyncMessageStatusDto.setInventoryList(warehouseDtoList);
        asyncMessageStatusDto.setOperateId(warehouseList.get(0).getTransferId());
        asyncMessageStatusDto.setOperateName(warehouseList.get(0).getTransferName());
        return asyncMessageStatusDto;
    }
}
